package com.youth.platform.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youth.framework.core.InvokeResult;
import com.youth.framework.util.MD5Util;
import com.youth.platform.entity.User;
import com.youth.platform.service.UserService;

@Controller
@Scope("prototype")
@RequestMapping("/lognController")
public class LoginController {

	@Resource(name = "userService")
	private UserService userService;

	@RequestMapping("/login.do")
	@ResponseBody()
	public InvokeResult<String> login(HttpServletRequest request) {
		InvokeResult<String> result = new InvokeResult<String>();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		password = MD5Util.MD5(password);
		User user = new User();
		user.setUserName(username);
		user = userService.selectUser(user);
		if (user == null) {
			result.setSuccessful(false);
			result.setMessage("输入的用户名不存在");
		} else {
			// 找到用户了校验用户名称和密码 
			if (!user.getUserPassword().equals(password)) {
				result.setSuccessful(false);
				result.setMessage("密码不正确");
			} else {
				result.setSuccessful(true);
			}
		}
		HttpSession session = request.getSession(); // 得到session
		session.setAttribute("user_session", user);
		request.setAttribute("user", user);

		return result;

	}

	@RequestMapping("/index.do")
	public ModelAndView index(HttpServletRequest request) {

		ModelAndView m = new ModelAndView("index/index");

		return m;

	}

	@RequestMapping("/logno.do")
	public ModelAndView loginIndex(HttpServletRequest request) {

		ModelAndView m = new ModelAndView("../login");

		return m;

	}
}
